package com.huaxin.hxcloud.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.PlanContentDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.utils.SearchByKey;
import com.huaxin.common.utils.SpacesItemDecoration;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/5/23
 * @Desc $desc$ 力鼎预案内容
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxLDPlanContentActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.et_search_input)
    EditText     et_search;
    @BindView(R.id.img_search_icon)
    ImageView    search_icon;
    @BindView(R.id.rl_plan_content)
    RecyclerView rl_plan;
    private LDPlanContentAdapter ldPlanContentAdapter;
    private List<PlanContentDTO.PlanCotent> contentDatas = new ArrayList<>();
    private int    planType;
    private View   emptyView;
    private String title;
    private TextView tv_tip;

    public static void start(Context context, int planType, String title) {
        Intent intent = new Intent(context, HxLDPlanContentActivity.class);
        intent.putExtra(Constants.IK_PLAN_TYPE, planType);
        intent.putExtra(Constants.IK_WT_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        if (extras.containsKey(Constants.IK_PLAN_TYPE)) {
            planType = extras.getInt(Constants.IK_PLAN_TYPE);
        } else {
            finish();
        }
        title = extras.getString(Constants.IK_WT_TITLE);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.act_ldplan_content;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(title);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_plan.setLayoutManager(manager);
        ldPlanContentAdapter = new LDPlanContentAdapter(R.layout.item_plan_content, contentDatas);
        rl_plan.setAdapter(ldPlanContentAdapter);
        rl_plan.addItemDecoration(new SpacesItemDecoration(30));
        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) rl_plan.getParent(), false);
        tv_tip = (TextView) emptyView.findViewById(R.id.tv_tip);

        ldPlanContentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_read) {
                    PlanContentDTO.PlanCotent planCotent = contentDatas.get(position);
                    HxLDReadContentActivity.start(HxLDPlanContentActivity.this, planCotent.getPlanContent(), planCotent.getPlanTitle());
                }
            }
        });
        ldPlanContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PlanContentDTO.PlanCotent planCotent = contentDatas.get(position);
                HxLDReadContentActivity.start(HxLDPlanContentActivity.this, planCotent.getPlanContent(), planCotent.getPlanTitle());
            }
        });

        //自动模糊搜索
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (contentDatas == null || contentDatas.size() <= 0) {
                    ToastUtil.showToast("没搜索到结果");
                    return;
                }
                String searchKey = et_search.getText().toString().trim();
                //通过关键字搜索
                search(searchKey);

            }
        });

    }

    /**
     * 本地搜索
     *
     * @param searchKey
     */
    private void search(String searchKey) {
        List<PlanContentDTO.PlanCotent> searchContents = SearchByKey.filterData(searchKey, contentDatas);
        if (searchContents != null && searchContents.size() > 0) {
            ldPlanContentAdapter.setNewData(searchContents);
        } else {
            ldPlanContentAdapter.setNewData(null);
            ldPlanContentAdapter.setEmptyView(emptyView);
            tv_tip.setText("");
            ToastUtil.showToast("没搜索到结果");
        }
    }

    @Override
    protected void requestData() {
        apiFindTablePlan();
    }

    private void apiFindTablePlan() {
        updateLoadStatus(LOADING);
        Call<PlanContentDTO> call = HxTransmitRequestHelper.getRequestApi().getFindTablePlan(planType);
        autoClose(call);
        call.enqueue(new Callback<PlanContentDTO>() {

            @Override
            public void onResponse(Call<PlanContentDTO> call, Response<PlanContentDTO> response) {
                PlanContentDTO contentDTO = response.body();
                if (contentDTO != null && contentDTO.isSuccessful()) {
                    updateLoadStatus(LOAD_SUCCESS);
                    contentDatas = contentDTO.getData();
                    if (contentDatas != null && contentDatas.size() > 0) {
                        ldPlanContentAdapter.setNewData(contentDatas);
                    } else {
                        ldPlanContentAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<PlanContentDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    class LDPlanContentAdapter extends BaseQuickAdapter<PlanContentDTO.PlanCotent, BaseViewHolder> {


        public LDPlanContentAdapter(int layoutResId, @Nullable List<PlanContentDTO.PlanCotent> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, PlanContentDTO.PlanCotent planCotent) {
            TextView tv_title = helper.getView(R.id.tv_plan_content_title);
            TextView tv_read = helper.getView(R.id.tv_read);

            if (planCotent.getPlanTitle_html() != null) {
                tv_title.setText(Html.fromHtml(planCotent.getPlanTitle_html()));
            } else {
                tv_title.setText(planCotent.getPlanTitle());
            }
            helper.addOnClickListener(R.id.tv_read);
        }

    }
}
